class Solution
{
public:
    int triangleNumber(vector<int>& nums)
    {
        int ret = 0;
        sort(nums.begin(), nums.end());

        for (int i = nums.size() - 1; i >= 2; i--)
        {
            int key = nums[i];
            int left = 0, right = i - 1;

            while (left < right)
            {
                if (nums[left] + nums[right] > key)
                {
                    ret += right - left;
                    right--;
                }
                else
                {
                    left++;
                }
            }

        }

        return ret;
    }
};